/**
 * Copyright (C) <2023> <Boundivore> <boundivore@foxmail.com>
 * <p>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Apache License, Version 2.0 for more details.
 * <p>
 * You should have received a copy of the Apache License, Version 2.0
 * along with this program; if not, you can obtain a copy at
 * http://www.apache.org/licenses/LICENSE-2.0.
 */
import React, { FC } from 'react';
import { t } from 'i18next';
import { Space, Button, Dropdown, Input } from 'antd';
import { UndoOutlined } from '@ant-design/icons';
import { useNodeDropdown } from '../hooks/useNodeDropdown';
const { Search } = Input;
interface NodeDropdownButtonProps {
	buttonText: string;
	callBack: (nodeId: string) => void;
}

export const NodeDropdownButton: FC<NodeDropdownButtonProps> = ({ buttonText, callBack }) => {
	const { dropDownItems, showDownloadList, handleSearch } = useNodeDropdown(callBack);

	return (
		<Dropdown
			menu={{ items: dropDownItems }}
			trigger={['click']}
			dropdownRender={menu => (
				<div className="bg-white rounded-lg shadow-md">
					<Search
						placeholder={t('inputHostName')}
						allowClear
						onSearch={value => handleSearch(value)}
						onChange={e => handleSearch(e.target.value)}
					/>
					{React.cloneElement(
						menu as React.ReactElement<{
							className: string;
						}>,
						{ className: 'shadow-none' }
					)}
				</div>
			)}
		>
			<Button type="primary" onClick={showDownloadList}>
				<Space>
					{buttonText}
					<UndoOutlined />
				</Space>
			</Button>
		</Dropdown>
	);
};
